//
//  OrderBeiZhuViewController.m
//  danpin
//
//  Created by chuangjia on 21/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderBeiZhuViewController.h"
#import "UIPlaceholderTextView.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"

@interface OrderBeiZhuViewController ()<UITextViewDelegate>
{
    
}
@property(nonatomic,strong)UIPlaceholderTextView *mTextView;
@property(nonatomic, strong) TTTAttributedLabel* ttitle;
@end

@implementation OrderBeiZhuViewController
@synthesize conpon=_conpon;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle =@"备注";
    self.rightTitle =@"保存";
    self.mTextView.frame= CGRectMake(PHOTO_FRAME_WIDTH, hightAuto(80), DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*10);
    [self.view addSubview:self.mTextView];
    if (_conpon.length>0) {
        self.mTextView.text = _conpon;
    }
    
    NSString *cstr =@"优惠码使用说明:\n1.此处填写商家的折扣代码，保存备注信息后，请先按照原价支付;\n2.若折扣码可以使用，我们的订单团队处理后将退回折扣码金额;\n3.若折扣码不能使用，我们将取消您的订单；如无论如何都要买到该商品，请勾选“如折扣码无法使用，原价下单”按钮。\n";
    [self.ttitle setText:cstr afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
     {
         [attributedString addAttribute:NSFontAttributeName
                                  value:FONT(12)
                                  range:NSMakeRange(0,[attributedString length])];
         //设置字体颜色
         [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0,[attributedString length])];
         return attributedString;
         
     }];
    
    float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*10) str:self.ttitle.attributedText];
    self.ttitle.frame = CGRectMake(PHOTO_FRAME_WIDTH, hightAuto(80)+PHOTO_FRAME_WIDTH*10,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*2, fheight);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    self.actionBlock =nil;
    self.mTextView=nil;
}
-(void)setCallBack:(void(^)(UIView*,id))callback
{
    self.actionBlock =callback;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            [self newAction];
        }
            break;
        default:
            break;
    }
}
-(void)newAction
{
    //    self.mTextView.text=@"http://detail.tmall.com/item.htm?id=43765502699";
    NSString *text =self.mTextView.text;
    if (text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请填写商家折扣码/优惠券!"];
        return;
    }
    if (self.actionBlock) {
        self.actionBlock(self.view,text);
    }
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 1) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(TTTAttributedLabel*)ttitle
{
    if (!_ttitle) {
        _ttitle =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*8,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH* 10,40)];
        _ttitle.backgroundColor = [UIColor clearColor];
        _ttitle.textColor = [UIColor lightGrayColor];
        _ttitle.textAlignment = NSTextAlignmentLeft;
        _ttitle.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _ttitle.numberOfLines=0;
        _ttitle.font = FONT(12);
        [self.view addSubview:_ttitle];
    }
    return _ttitle;
}
//
-(UIPlaceholderTextView*)mTextView
{
    if (!_mTextView) {
        self.mTextView =[[UIPlaceholderTextView alloc]initWithFrame:CGRectZero];
        self.mTextView.backgroundColor =[UIColor clearColor];
        self.mTextView.delegate = self;
        self.mTextView.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        self.mTextView.layer.borderWidth =0.5;
        self.mTextView.layer.cornerRadius=3;
        self.mTextView.placeholder = @"可填写商家折扣码/优惠券";
        self.mTextView.textColor = [UIColor darkGrayColor];
        self.mTextView.font =FONT(14);
        self.mTextView.returnKeyType = UIReturnKeyDone;
        [self.mTextView becomeFirstResponder];
    }
    return _mTextView;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}

@end
